package com.sharding.quickStart;

import com.alibaba.fastjson.JSON;
import com.sharding.entity.Account;
import com.sharding.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Desc:`
 * Date:    2023/2/26
 *
 * @author WanYangWei
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class ModShardingDemo {

    @Autowired
    AccountService accountService;

    private final String[] names = {"张三", "李四", "王五", "赵六"};

    @Test
    public void save() {
        Account account = build();
        for (int i = 0; i < 10; i++) {
            account.setId(RandomUtils.nextInt());
            account.setBalance(RandomUtils.nextInt());
            account.setName(names[i % names.length] + i);
            accountService.save(account);
        }
    }

    @Test
    public void getAll() {
        List<Account> accountList = accountService.selectAll();
        LinkedHashSet<Long> collect = accountList.stream().map(Account::getId).collect(Collectors.toCollection(LinkedHashSet::new));
        log.info("getAllInfo:{} ",collect);
    }


    @Test
    public void getById() {
        Account account = accountService.selectById(1L);

        log.info("getByIdInfo:{}", JSON.toJSONString(account));

    }
    private Account build() {
//        Account account=new Account();
        return new Account();
    }

}
